# -*- coding: utf-8 -*-

import json
import re

class Codes:
    OK = 0
    NOT_FOUND = -1
    PARTIAL = -2
    URL_PARTIAL = -3
    DB_ERROR = -4
    PARAM_ERROR = -5

class Utils:
    CODE_OK = 0
    CODE_ERR = -1

    @classmethod
    def genResponse(cls, code, msg, data):
        responseData = {}
        responseData['code'] = code
        responseData['msg'] = msg
        responseData['data'] = data
        return json.dumps(responseData, ensure_ascii=False, encoding='utf-8')

    @classmethod
    def extract_keywords(cls, origin, regular):
        if origin is None:
            return None
        ret = re.findall(regular, origin)
        if len(ret) > 0:
            return ret[0]
        else:
            return None

    @classmethod
    def str_is_num(cls, str):
        try:
            n = int(str)
        except:
            return False
        else:
            return True